package com.chance.cc.crawler.development.bootstrap.weixin;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Html;
import com.chance.cc.crawler.core.selector.Json;
import com.chance.cc.crawler.core.selector.Selectable;
import com.chance.cc.crawler.development.bootstrap.xcar.XCarHttpTest;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bx
 * @date 2021/2/18 0018 15:36
 */
public class WXGZHTest {
    public static Logger logger = LoggerFactory.getLogger(XCarHttpTest.class);


    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {

        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i=1;i<=1;i++){
            new Thread(new TestRun(downloader,httpConfig),"thread_"+i).start();
        }
    }

    static class TestRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            try {
                List<String> allLines = IOUtils.readLines(new FileInputStream("H:\\常用软件\\微信\\wecat\\WeChat Files\\wxid_fk4b1kfjy4l922\\FileStorage\\File\\2021-02\\wx_gzh_finally_0224(1).txt"),"UTF-8");
                FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\jar\\wx\\gzh_all_1.json",true);
                for (String line : allLines) {
                    if (line.startsWith("#")){
                        continue;
                    }
                    String outputLine  = line;
                    Json json = new Json(line);
                    String biz = json.jsonPath("$.biz").get();
                    System.out.println("提取 biz :" + biz);
                    if (StringUtils.isNotBlank(biz) && !biz.equals("null")){
                        Map map = JSON.parseObject(line, Map.class);
                        Thread.sleep(4000);
                        map.put("about_biz",gzhDetail(biz,downloader,httpConfig));
                        Thread.sleep(4000);
                        map.put("account_subject",accountDetail(biz,downloader,httpConfig));
                        outputLine = JSON.toJSONString(map);
                    }
                    IOUtils.write(outputLine,outputStream,"utf-8");
                    outputStream.write("\n".getBytes());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            gzhDetail("MjM5NzA1OTcxMg==",downloader,httpConfig);
//            accountDetail("MjM5NzA1OTcxMg==",downloader,httpConfig);


            // GZH DETAIL


            // ZH DETAIL
//            String url = "https://www.xiaohongshu.com/api/sns/v10/search/notes?"+ URLEncodedUtils.format(nameValuePairs,"utf-8");

        }

        private Map<String,Object> gzhDetail(String biz,HttpClientDownloader downloader,HttpConfig httpConfig){
            String gzhUrlFormat = "https://mp.weixin.qq.com/mp/aboutbiz?__biz=%s&devicetype=android-29&" +
                    "version=28000159&lang=zh_CN&nettype=WIFI&ascene=3&" +
                    "pass_ticket=EGmaxmKMUSQUtMWK2gaxvr%%2FMdjnvr7oBkPe4%%2BOqfhWV5R77GR2fjDR%%2F%%2FZeY%%2FW2WC" +
                    "&wx_header=1";

            String gzhUrl = String.format(gzhUrlFormat, biz);
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setUrl(gzhUrl);
            httpRequest.addHeader("Host","mp.weixin.qq.com");
            httpRequest.addHeader("Connection","keep-alive");
            httpRequest.addHeader("Upgrade-Insecure-Requests","1");
            httpRequest.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 10; TAS-AN00 Build/HUAWEITAS-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2759 MMWEBSDK/201201 Mobile Safari/537.36 MMWEBID/7650 MicroMessenger/8.0.1.1841(0x28000159) Process/toolsmp WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64");
            httpRequest.addHeader("Sec-Fetch-User","?1");
            httpRequest.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpic,image/tpg,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            httpRequest.addHeader("x-wechat-key","f805dda8c247794b9763e86235da1b8605fdb25e1dad5aad0c4bd1820ad732f33a43d85ee5e198dac3765367b5f6dc15f3de0a4fa9bff32857413a8dd24631a23390d041c4d028eef10162541b095303d20bcc1f4984924d8163d24a855e5edf3103f13fe40d1e0d8c3cc452cb467b1968ea0748f5b64fe4ef43fcfdf03f1989");
            httpRequest.addHeader("x-wechat-uin","MjUxNzA4MTk1");
            httpRequest.addHeader("X-Requested-With","com.tencent.mm");
            httpRequest.addHeader("Sec-Fetch-Site","none");
            httpRequest.addHeader("Sec-Fetch-Mode","navigate");
            httpRequest.addHeader("Accept-Encoding","gzip, deflate");
            httpRequest.addHeader("Accept-Language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
//            httpRequest.addHeader("Cookie","wxuin=773013646; devicetype=android-23; version=27000334; lang=zh_CN; pass_ticket=KxvZ2bWm1u6nLUHCPxBVhCSOUzzKYJ72UNqChGtp6PcxfgKngftPJO68dyPwEACj; wap_sid2=CI6BzfACEnZ5X0hQa2ZmLTBQNU1wVi0wR2xHc05Vb0tETnlaVFNRNm15QzVrTzJpRVdzSVdnb2tPZ1FuLWFyZ1FjTTc0clI3Z013dHZCYUh4cjRaRmpOQlVqLVV0ZlFsQmRLT2wyNXlSaUNXQ2w1OUFhVVdnekt4SUFBQX5+MNf43IEGOAxAlE4=");
            HttpPage httpPage = downloader.download(httpRequest, httpConfig);
            Html html = httpPage.getHtml();

            Map<String,Object> aboutBiz = new HashMap<>();
            List<Selectable> nodes = html.xpath("//div[@class=\"item-info\"]").nodes();
            if (nodes == null || nodes.size() < 0){
                logger.info("about biz download page is null!");
                return null;
            }
            for (Selectable node : nodes) {
                String title =  node.xpath("./div[@class=\"item-title\"]").get().trim();
                String desc = node.xpath("./div[@class=\"item-desc\"]").get().trim();
                aboutBiz.put(title,desc);
                if (node.xpath("./div[@class=\"item-title\"]").get().equals("名称记录")){
                    List<String> names = node.xpath("./div[@class=\"item-desc\"]/p/text()").all();
                    List<String> nameList= new ArrayList<>();
                    for (String name : names) {
                        nameList.add(StringEscapeUtils.unescapeHtml(StringEscapeUtils.unescapeHtml(name.trim())));
                    }
                    aboutBiz.put(title,nameList);
                }

            }

            Selectable node = html.xpath("//div[@class=\"item-info spacial\"]").nodes().get(0);
            String title =  node.xpath("./div[@class=\"item-title\"]").get().trim();
            String desc = StringUtils.joinWith("",node.xpath(".//a[@class=\"type-link-wrp item-desc\"]/text()").all().toArray()).trim();
            aboutBiz.put(title,desc);
            return aboutBiz;
        }

        private Map<String,Object> accountDetail(String biz,HttpClientDownloader downloader,HttpConfig httpConfig){
            String accountUrlFormat = "https://mp.weixin.qq.com/mp/getverifyinfo?__biz=%s&type=reg_info&" +
                    "devicetype=android-23&version=27000334&lang=zh_CN&nettype=WIFI&ascene=3&" +
                    "pass_ticket=EGmaxmKMUSQUtMWK2gaxvr%%2FMdjnvr7oBkPe4%%2BOqfhWV5R77GR2fjDR%%2F%%2FZeY%%2FW2WC" +
                    "&wx_header=1";

            String accountUrl = String.format(accountUrlFormat, biz);
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setUrl(accountUrl);

            httpRequest.addHeader("Host","mp.weixin.qq.com");
            httpRequest.addHeader("Connection","keep-alive");
            httpRequest.addHeader("Upgrade-Insecure-Requests","1");
            httpRequest.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; DIG-AL00 Build/HUAWEIDIG-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2758 MMWEBSDK/190102 Mobile Safari/537.36 MMWEBID/9405 MicroMessenger/7.0.3.1400(0x27000334) Process/toolsmp NetType/WIFI Language/zh_CN");
            httpRequest.addHeader("Sec-Fetch-User","?1");
            httpRequest.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpic,image/tpg,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
            httpRequest.addHeader("x-wechat-key","f805dda8c247794b9763e86235da1b8605fdb25e1dad5aad0c4bd1820ad732f33a43d85ee5e198dac3765367b5f6dc15f3de0a4fa9bff32857413a8dd24631a23390d041c4d028eef10162541b095303d20bcc1f4984924d8163d24a855e5edf3103f13fe40d1e0d8c3cc452cb467b1968ea0748f5b64fe4ef43fcfdf03f1989");
            httpRequest.addHeader("x-wechat-uin","MjUxNzA4MTk1");
            httpRequest.addHeader("X-Requested-With","com.tencent.mm");
            httpRequest.addHeader("Sec-Fetch-Site","none");
            httpRequest.addHeader("Sec-Fetch-Mode","navigate");
            httpRequest.addHeader("Accept-Encoding","gzip, deflate");
            httpRequest.addHeader("Accept-Language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
//            httpRequest.addHeader("Cookie","wxuin=773013646; devicetype=android-23; version=27000334; lang=zh_CN; pass_ticket=KxvZ2bWm1u6nLUHCPxBVhCSOUzzKYJ72UNqChGtp6PcxfgKngftPJO68dyPwEACj; wap_sid2=CI6BzfACEnZ5X0hMTWxnT2dwaUQ3bHQwZlhpRU1rTFFCNDllTVk4RV9sWi1zLVpmNkpSWERzVGtYMWhyLXgwek5mUjdiRGdUNFYyOElQWVlrNDNzdTlCa2dxNmRaNFlURlEzTWxtcFFHQkJ4LTIxN2R5NmdiN0t4SUFBQX5+MOvd3IEGOAxAlE4=");

            HttpPage httpPage = downloader.download(httpRequest, httpConfig);

            Map<String,Object> accountSubject = new HashMap<>();
            List<Selectable> nodes = httpPage.getHtml().xpath("//li[@class=\"verify_item\"]").nodes();
            if (nodes == null || nodes.size() < 1){
                logger.info("account subject download page is null!");
                return null;
            }
            for (Selectable node : nodes) {
                String title = node.xpath("./h3[@class=\"verify_item_title\"]").get().trim();
                if (node.xpath("./h3[@class=\"verify_item_title\"]").get().equals("该帐号部分功能由以下服务商提供")){
                    String dataStr = "(\\[[^\\]]*\\])";
                    String dataInfo = null;
                    Matcher mtAuthor = Pattern.compile(dataStr).matcher(httpPage.getRawText());
                    if (mtAuthor.find()){
                        dataInfo = mtAuthor.group();
                        List<String> accountInfos = new Json(dataInfo).toList(String.class);
                        accountSubject.put(title,accountInfos);
                    }
                } else if (node.xpath("./h3[@class=\"verify_item_title\"]").get().equals("名称记录")){
                    List<String> names = node.xpath("./div[@id=\"verify_item_desc js_div_rename_history\"]/p/text()").all();
                    List<String> nameList= new ArrayList<>();
                    for (String name : names) {
                        nameList.add(StringEscapeUtils.unescapeHtml(StringEscapeUtils.unescapeHtml(name.trim())));
                        accountSubject.put(title,nameList);
                    }
                } else {
                    String desc = node.xpath("./div[@class=\"verify_item_desc\"]/text()").get().trim();
                    accountSubject.put(title,desc);
                }
            }

            return accountSubject;
        }
    }



}
